package cpu.footprint.api;

import com.google.gson.Gson;

import org.xutils.common.Callback.Cancelable;
import org.xutils.common.Callback.CommonCallback;
import org.xutils.http.RequestParams;
import org.xutils.x;
import java.util.Map;

/**
 * Created by wjp on 2017/7/6.
 */
public class APIService {
    // api服务器
    /**
     * 发送get请求
     * @param <T>
     */
    public static <T> Cancelable Get(String url, Map<String,String> map, CommonCallback<T> callback){
        RequestParams params=new RequestParams(url);
        if(null!=map){
            for(Map.Entry<String, String> entry : map.entrySet()){
                params.addQueryStringParameter(entry.getKey(), entry.getValue());
            }
        }
        params.setConnectTimeout(10000);
        Cancelable cancelable = x.http().get(params, callback);
        return cancelable;
    }

    /**
     * 发送post请求
     * @param <T>
     */
    public static <T> Cancelable Post(String url, Map<String,String> map, RespCallBack<T> callback){
        RequestParams params=new RequestParams(url);
//        params.addHeader("Content-Type", "application/json");
//        params.setAsJsonContent(true);
//        String json = new Gson().toJson(map);
//        params.setBodyContent(json.toString());
        params.setConnectTimeout(10000);
        if(null!=map){
            for(Map.Entry<String, String> entry : map.entrySet()){
                params.addBodyParameter(entry.getKey(), entry.getValue());
            }
        }
        //添加请求参数
        Cancelable cancelable = x.http().post(params, callback);
        return cancelable;
    }

    /**
     * 同步访问
     * @param url
     * @param map
     * @param resultType
     * @param <T>
     * @return
     */
    public static <T> T postSync(String url, Map<String,Object> map, Class<T> resultType){
        RequestParams params=new RequestParams(url);
        params.addHeader("Content-Type", "application/json");
        params.setAsJsonContent(true);
        String json = new Gson().toJson(map);
        params.setBodyContent(json.toString());
        try {
            T t=x.http().postSync(params,resultType);
            return t;
        } catch (Throwable throwable) {
            throwable.printStackTrace();
        }
        return null;
    }
    /**
     * 上传文件
     * @param <T>
     */
    public static <T> Cancelable upLoadFile(String url,Map<String,Object> map,CommonCallback<T> callback){
        RequestParams params=new RequestParams(url);
        //添加请求头
        if(null!=map){
            for(Map.Entry<String, Object> entry : map.entrySet()){
                params.addParameter(entry.getKey(), entry.getValue());
            }
        }
        params.setMultipart(true);
        Cancelable cancelable = x.http().post(params, callback);
        return cancelable;
    }

    /**
     * 下载文件
     * @param <T>
     */
    public static <T> Cancelable DownLoadFile(String url,String filepath,CommonCallback<T> callback){
        RequestParams params=new RequestParams(url);
        //设置断点续传
        params.setAutoResume(true);
        params.setSaveFilePath(filepath);
        Cancelable cancelable = x.http().get(params, callback);
        return cancelable;
    }
}
